import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/Home'
import Login from '@/views/Login'
import Cate from '@/components/Cate'
import Data from '@/components/Data'
import Dingdan from '@/components/Dingdan'
import Goods from '@/components/Goods'
import Params from '@/components/Params'
import Righrs from '@/components/Righrs'
import Rules from '@/components/Rules'
import Users from '@/components/Users'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    children: [{
      path: '/users',
      name: 'users',
      component: Users
    },
    {
      path: '/roles',
      name: 'roles',
      component: Rules
    },
    {
      path: '/rights',
      name: 'rights',
      component: Righrs
    },
    {
      path: '/params',
      name: 'params',
      component: Params
    },
    {
      path: '/goods',
      name: 'goods',
      component: Goods
    },
    {
      path: '/orders',
      name: 'orders',
      component: Dingdan
    },
    {
      path: '/reports',
      name: 'reports',
      component: Data
    },
    {
      path: '/categories',
      name: 'categories',
      component: Cate
    }
    ]
  }
]
const router = new VueRouter({
  routes
})
// 配置路由全局守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (token) return next()
  if (from.path !== '/login') router.push('/login')
})
export default router
